#!/bin/bash
#THIS SCRIPT DESIGNED TO TAKE THE OFF LINE CODE PRODUCED BY MATT QUOK'S INTERFACE
#AND TRANSLATE THIS TO A CONSISTENT NAMING CONVENTION FOR THE RAW DATA. WHEN USED IN MODE
#THE FOLLOWING SYNTAX IS APPLICABLE:
#bash $0 sourcecode [code value] targetcode
#In this instance, the value returned is a string such as "--MAINT--" or "--AUDIT--" 
#WHEN USED TO TRANSLATE INTO AN OP CODE BY ingest.sh OR dmsout.sh, THE FOLLOWING SYNTAX
#IS APPLICABLE
#bash $0 opcode [interface-offline code]
#In this instance the value returned is "51,52,53,60,63,64,66,or 68"
#---------------------------------------------------------------------------------------
#TO INCLUDE THIS SCRIPT TYPE "source sys_resources/off_line_cal.sh" into the top 
#of any driver or script

#ERROR CHECKING
if [ ${#} != 3 ]; then
	{
		echo "Sorry, not able to run without three parameters"
	exit 1 
	}
elif [ "${1}" != "rawcode" ] && [ "${1}" != "opcode" ] && [ "${1}" != "displaycode" ] && [ "${1}" != "dmscode" ]; then
	{
		echo "Sorry the first variable must be either rawcode, opcode, displaycode, dmscode"	
		exit 1
	}
elif [ "${3}" != "rawcode" ] && [ "${3}" != "opcode" ] && [ "${3}" != "displaycode" ] && [ "${3}" != "dmscode" ]; then
	{
		echo "Sorry the third variable must be either rawcode, opcode, displaycode, dmscode"	
		exit 1
	}
fi 
##############################################################################################
#                                  CONVERSION ARRAY DEFINITIONS                              #
# Must be 1 to 1 to work properly! Each code must have a corresponding entry in all 3        #
# even if blank.  																			 #
# When adding to arrays, make sure arrays and the definition_count variable are updated      #
##############################################################################################
definition_count=7
displaycode=( 0 1 2 3 4 5 6 )
opcode=( 0 53 51 52 68 60 64 )
rawcode=( "" --OFFLINE-- --MAINTENANCE-- --REPAIR-- --CALIBRATION-- --AUDIT-- --ZERO-- )
dmscode=( 0 1 2 3 4 5 6 )
##############################################################################################
#   Conversion Function																		 #
##############################################################################################

array_count=0
terminate_loop=0
#definition_count=`expr $definition_count + 0`
#array_count=`expr $array_count + 0`
while [ $array_count -lt $definition_count ] && [ $terminate_loop -eq 0 ]; do
	temp_name_source="$1[$array_count]"
	if [ "${!temp_name_source}" = "$2" ]; then
		temp_name_target="$3[$array_count]"
		target_value=${!temp_name_target}
		#echo ${!temp_name_source}  ##DEBUG
		#echo $target_value         ##DEBUG
		terminate_loop=1
	else
		array_count=`expr $array_count + 1`
	fi
done


